package com.sunny.binary;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 二进制的方法来找所有的子集
 */
public class Main {

    public static ArrayList<Set<Integer>> solve(List<Integer> set){

        ArrayList<Set<Integer>> list = new ArrayList<>();

        int n = set.size();
        int max = 1<<n;
        for(int i=0;i<max;i++){
            list.add(getSetFromInt(set,i));
        }
        return list;
    }

    public static Set<Integer> getSetFromInt(List<Integer> set, int num){
        Set<Integer> res = new HashSet<>();
        int i=0;
        while (num != 0){
            if((num&1) > 0){
                res.add(set.get(i));
            }
            num >>= 1;
            i++;
        }
        return res;
    }

    public static void print(ArrayList<Set<Integer>> list){

        list.forEach(s->{
            s.forEach(a->{
                System.out.print(a+" ");
            });
            System.out.println();
        });

    }

    public static void main(String[] args) {

        List<Integer> set = Arrays.asList(1,2,3);
        print(solve(set));

    }

}
